﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TTBusiness.DB_Linq;
namespace TTBusiness.ClassDB
{
    public class clsBSCateAttribute
    {
        TTMarketing_DBDataContext objDB = new TTMarketing_DBDataContext();
        public List<tblCate_Attribute> getAll()
        {
            try
            {
                var list = from a in objDB.tblCate_Attributes
                           select a;
                return list.ToList<tblCate_Attribute>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblAttribute> getByProductId(int productid)
        {
            try
            {
                var list = from p in objDB.tblProducts
                           where p.ID==productid
                           select p into product
                           from a in objDB.tblCate_Attributes
                           where a.tblCateProduct.ID==product.tblCate_Factory.CateID
                           select a.tblAttribute;
                return list.ToList<tblAttribute>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblAttribute getAttributeByID(int cateAttributeId)
        {
            try
            {
                var a = (from ca in objDB.tblCate_Attributes
                         where ca.ID.Equals(cateAttributeId)
                         select ca.tblAttribute).SingleOrDefault();
                return a;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<tblCate_Attribute> getByCate(int cateId)
        {
            try
            {
                var list = from a in objDB.tblCate_Attributes
                           where a.CateID.Equals(cateId)
                           select a;
                return list.ToList<tblCate_Attribute>();
            }
            catch (Exception)
            {

                throw;
            }
        }
        public tblCate_Attribute getById(int id)
        {
            try
            {
                var obj = objDB.tblCate_Attributes.SingleOrDefault(a => a.ID == id);
                return obj;
                
               
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        public tblCate_Attribute getByAttributeAndProduct(int attributeid,int productid)
        {
            try
            {
                var obj = (from p in objDB.tblProducts
                           where p.ID == productid
                           select p into product
                           from a in objDB.tblCate_Attributes
                           where a.AttributeID == attributeid && a.CateID == product.tblCate_Factory.CateID
                           select a).Single();
                return obj;

            }
            catch (Exception)
            {

                throw;
            }
        }

        public void insert(tblCate_Attribute obj)
        {
            try
            {
                objDB.tblCate_Attributes.InsertOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void update(tblCate_Attribute obj)
        {
            try
            {
                var objj = objDB.tblCate_Attributes.SingleOrDefault(a => a.ID == obj.ID);
                if (objj != null)
                {
                    objj.ID = obj.ID;
                    objj.CateID = obj.CateID;
                    objj.AttributeID = obj.AttributeID;
                    objj.Number = obj.Number;
                    objDB.SubmitChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void delete(int id)
        {
            try
            {
                var obj = (from a in objDB.tblCate_Attributes
                           where a.ID == id
                           select a).SingleOrDefault();
                objDB.tblCate_Attributes.DeleteOnSubmit(obj);
                objDB.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
